<?php
/*
    ID:   $Id: $
    Name: John Griffin
    Date: April 29,2009
*/
$dir = preg_replace ('/\/trunk\/.*/', '/trunk', __FILE__);
ini_set ('include_path', $dir.'/source:'.$dir.'/test:'.ini_get ('include_path'));

require_once 'simpletest/autorun.php';
require_once 'etc/config.php';
require_once 'lib/Error.php';
require_once 'lib/taskModel.php';

class TestTaskModel extends UnitTestCase {
    private $pdo = null;
    private $error = null;

    public function __construct () {
        $this->pdo = new PDO (DATABASE_CONNECT_STRING, DATABASE_USER, DATABASE_PASSWORD);
        $this->error = new Error ();
    }

    public function testInvoke () {
        $model = new TaskModel ($this->pdo, $this->error);
    }
    
    // test with no arguments
    public function testQueryFilter1 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ();
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    // test integers
    public function testQueryFilter2 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id = 1";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => 1);
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter3 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id >= 1";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '[1:]');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter4 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id <= 1";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '[:1]');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter5 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id BETWEEN 1 AND 2";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '[1:2]');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter6 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id IN (1)";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '{1}');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter7 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id IN (1,2)";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '{1,2}');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
        
    public function testQueryFilter8 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE id IN (1, 2)";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('id' => '{1, 2}');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }

    // test dates
    public function testQueryFilter20 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE modified = '2000-01-01'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('modified' => '2000-01-01');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }

    public function testQueryFilter21 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE modified >= '2000-01-01'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('modified' => "[2000-01-01:]");
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }

    public function testQueryFilter22 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE modified <= '2000-01-01'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('modified' => "[:2000-01-01]");
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }

    public function testQueryFilter23 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE modified BETWEEN '2000-01-01' AND '2000-01-02'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('modified' => "[2000-01-01:2000-01-02]");
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }

    public function testQueryFilter24 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE modified IN ('2000-01-01', '2000-01-02', '2000-01-03')";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('modified' => "{2000-01-01,2000-01-02,2000-01-03}");
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    // test strings
    public function testQueryFilter40 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE subject = 'simple string'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('subject' => 'simple string');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter41 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE subject LIKE 'simple % string'";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('subject' => 'simple % string');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testQueryFilter42 () {
        $expected = "SELECT id, modified, subject, path, body FROM bd_task WHERE subject IS NULL";
        $model = new TaskModel ($this->pdo, $this->error);
        $filter = array ('subject' => '');
        $actual = $model->query ($filter);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
}

?>
